//防抖
export const debounce = (fn, t, immediate = false) => {
    const delay = t || 500
    let timer

    return function () {
        const args = arguments
        if (timer) {
            clearTimeout(timer)
        }

        timer = setTimeout(() => {
            timer = null
            fn.apply(this, args)
        }, delay)
        if (immediate) {
            immediate = false
            fn.apply(this, args)
        }
    }
}

//节流
export const throttle = (fn, t) => {
    let timer = null
    const interval = t || 1000

    return function () {
        const args = arguments
        if (!timer) {
            fn.apply(this, args)
            timer = setTimeout(() => {
                timer = null
            }, interval)
        }
    }
}